package com.gdit.zax.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.dao.CollectDao;
import com.gdit.zax.dao.SignDao;
import com.gdit.zax.entitys.Dynamic;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.DateUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.VeriFicUtil;

public class SigninTable extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String accesstoken = request.getParameter("accesstoken");
		int year = TextUtil.String2int(request.getParameter("year"), -1);
		int month = TextUtil.String2int(request.getParameter("month"), -1);
		
		//检测参数
		if (TextUtil.isEmpty(accesstoken)) {
			O.printFail(response, 1, "请检查accesstoken参数!");
			return;
		}
		
		if (year < 0) {
			O.printFail(response, 1, "请检查year参数!");
			return;
		}
		
		if (month < 0) {
			O.printFail(response, 1, "请检查month参数!");
			return;
		}
		
		//检测连接、用户
		int uid = VeriFicUtil.getVFID(accesstoken);
		Connection conn = DBUtil.getConnection();
		
		if (uid==Integer.MIN_VALUE || uid==Integer.MAX_VALUE) {
			O.printNoLogin(response);
			return;
		}
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao = DAOFactory.init(conn, request, response);
		SignDao sdao = dao.getSignDao();
		ArrayList<Dynamic> list = sdao.listsignbytime(uid, year, month);
		ArrayList<Map> outlist = new ArrayList<Map>();
		for (Dynamic dy:list) {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = DateUtil.getDate(dy.getAddtime(), null);
			map.put("id", dy.getId());
			map.put("day", date.getDate());
			map.put("addtime", dy.getAddtime());
			outlist.add(map);
		}
		O.printSuccess(response, outlist);
		
	}

}
